<!DOCTYPE html>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<!-- three.js library -->
<script src='../../vendor/three.js/build/three.js'></script>
<!-- jsartookit -->
<script src="../vendor/jsartoolkit5/build/artoolkit.min.js"></script>
<script src="../vendor/jsartoolkit5/js/artoolkit.api.js"></script>
<!-- include threex.artoolkit -->
<script src="../threex-artoolkitcontext.js"></script>
<script src="../threex-armarkercontrols.js"></script>

<script src="../vendor/jsartoolkit5/js/artoolkit.proxy.js"></script>

<body style='margin : 0px;overflow: hidden;'><script>
	//////////////////////////////////////////////////////////////////////////////////
	//		Init
	//////////////////////////////////////////////////////////////////////////////////

	// init renderer
	var renderer	= new THREE.WebGLRenderer({
		antialias	: true,
		alpha: true
	});
	renderer.setClearColor(new THREE.Color('lightgrey'), 0)
	renderer.setSize( 640, 480 );
	renderer.domElement.style.position = 'absolute'
	renderer.domElement.style.top = '0px'
	renderer.domElement.style.left = '0px'
	document.body.appendChild( renderer.domElement );

	// array of functions for the rendering loop
	var onRenderFcts= [];

	// init scene and camera
	var scene	= new THREE.Scene();

	var ambient = new THREE.AmbientLight( 0x666666 );
	scene.add( ambient );

	var directionalLight = new THREE.DirectionalLight( 0x887766 );
	directionalLight.position.set( -1, 1, 1 ).normalize();
	scene.add( directionalLight );
	
	//////////////////////////////////////////////////////////////////////////////////
	//		Comments
	//////////////////////////////////////////////////////////////////////////////////

	// Create a camera and a marker root object for your Three.js scene.
	var camera = new THREE.Camera();
	scene.add(camera);

	// create arToolkitContext
	var arToolkitContext = new THREEx.ArToolkitContext({
		// sourceType : 'image',
		// sourceUrl : '../../../../data/images/img.jpg',
		// sourceType : 'video',
		// sourceUrl : '../../../../data/videos/headtracking.mp4',
		sourceType : 'webcam',
		// sourceUrl : '../../../../data/videos/headtracking.mp4',
		cameraParametersUrl: '../../../../data/data/camera_para.dat',
		detectionMode: 'mono'
	})

	// we init the source
	arToolkitContext.initSource(function(){
		// copy projection matrix to camera
	        var projectionMatrix = arToolkitContext.arController.getCameraMatrix();
	        camera.projectionMatrix.fromArray(projectionMatrix);
		// handle resize of renderer
		onResize()
		arToolkitContext._proxy.processingDone = true;
	})
	// handle resize
	window.addEventListener('resize', onResize)	
	function onResize(){

		// handle arToolkitContext resize
		arToolkitContext.onResize()
	
		// copy arToolkitContext.srcElement position to renderer.domElement
		var domElement = renderer.domElement
		var srcElement = arToolkitContext.srcElement
		domElement.style.width = srcElement.style.width
		domElement.style.height = srcElement.style.height	
		domElement.style.marginLeft = srcElement.style.marginLeft
		domElement.style.marginTop = srcElement.style.marginTop		
	}

	// update artoolkit on every frame
	onRenderFcts.push(function(){
		arToolkitContext.update()
	})


	// init controls for camera
	var markerControls = new THREEx.ArMarkerControls(arToolkitContext, camera, {
		type : 'pattern',
		// patternUrl : '../../../../data/data/patt.hiro',
		patternUrl : '../../../../data/data/patt.kanji',
		// as we controls the camera 
		changeMatrixMode: 'cameraTransformMatrix'
	})


	//////////////////////////////////////////////////////////////////////////////////
	//		add an object in the scene
	//////////////////////////////////////////////////////////////////////////////////

	// add a torus knot	
	var geometry	= new THREE.CubeGeometry(1,1,1);
	var material	= new THREE.MeshNormalMaterial({
		transparent : true,
		opacity: 0.5,
		side: THREE.DoubleSide
	}); 
	var mesh	= new THREE.Mesh( geometry, material );
	mesh.position.z	= geometry.parameters.height/2
	scene.add( mesh );
	
	var geometry	= new THREE.TorusKnotGeometry(0.3,0.1,32,32);
	var material	= new THREE.MeshNormalMaterial(); 
	var mesh	= new THREE.Mesh( geometry, material );
	mesh.position.z	= 0.5
	scene.add( mesh );
	
	onRenderFcts.push(function(){
		mesh.rotation.x += 0.1
	})

	//////////////////////////////////////////////////////////////////////////////////
	//		render the whole thing on the page
	//////////////////////////////////////////////////////////////////////////////////

	// render the scene
	onRenderFcts.push(function(){
		renderer.render( scene, camera );
	})

	// run the rendering loop
	var lastTimeMsec= null
	requestAnimationFrame(function animate(nowMsec){
		// keep looping
		requestAnimationFrame( animate );
		// measure time
		lastTimeMsec	= lastTimeMsec || nowMsec-1000/60
		var deltaMsec	= Math.min(200, nowMsec - lastTimeMsec)
		lastTimeMsec	= nowMsec
		// call each update function
		onRenderFcts.forEach(function(onRenderFct){
			onRenderFct(deltaMsec/1000, nowMsec/1000)
		})
	})
</script></body>
